package org.linlinjava.litemall.db.service;

import com.github.pagehelper.PageHelper;
import org.apache.ibatis.session.RowBounds;
import org.linlinjava.litemall.db.dao.LitemallRentOrderDetailMapper;
import org.linlinjava.litemall.db.domain.LitemallRentOrderDetail;
import org.linlinjava.litemall.db.domain.LitemallRentOrderDetailExample;
import org.linlinjava.litemall.db.util.RentNotReturnConstant;
import org.linlinjava.litemall.db.util.RentOrderConstant;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.concurrent.ExecutorService;

/**
 * @author Administrator
 */
@Service
public class LitemallRentOrderDetailService {
    @Resource
    private LitemallRentOrderDetailMapper rentOrderDetailMapper;

    public LitemallRentOrderDetail findByProductId(Integer productId) {
        LitemallRentOrderDetailExample example = new LitemallRentOrderDetailExample();
        example.or().andGoodsProductIdEqualTo(productId);
        return rentOrderDetailMapper.selectOneByExample(example);
    }

    public List<LitemallRentOrderDetail> queryByOid(Integer orderId) {
        LitemallRentOrderDetailExample example = new LitemallRentOrderDetailExample();
        example.or().andRentOrderIdEqualTo(orderId);
        return rentOrderDetailMapper.selectByExample(example);
    }

    public List<LitemallRentOrderDetail> queryByOidAndStatus(Integer orderId) {
        LitemallRentOrderDetailExample example = new LitemallRentOrderDetailExample();
        example.or().andRentOrderIdEqualTo(orderId).andStatusNotEqualTo(0).andDeletedEqualTo(false);
        return rentOrderDetailMapper.selectByExample(example);
    }

    public void updateById(LitemallRentOrderDetail rentOrderDetail) {
        rentOrderDetail.setUpdateTime(LocalDateTime.now());
        rentOrderDetailMapper.updateByPrimaryKeySelective(rentOrderDetail);
    }

    public List<LitemallRentOrderDetail> queryWaitByOid(Integer rentOrderId) {
        LitemallRentOrderDetailExample example = new LitemallRentOrderDetailExample();
        example.or().andRentOrderIdEqualTo(rentOrderId).andStatusEqualTo(RentNotReturnConstant.STATUS_WAITING).andDeletedEqualTo(false);
        List<LitemallRentOrderDetail> rentOrderDetailList = rentOrderDetailMapper.selectByExample(example);

        return rentOrderDetailList;
    }

    public List<LitemallRentOrderDetail> queryRecentRead(Integer userId) {
        LitemallRentOrderDetailExample example = new LitemallRentOrderDetailExample();
        example.or().andUserIdEqualTo(userId).andDeletedEqualTo(false);
        example.setOrderByClause("update_time desc");

        PageHelper.startPage(1, 3);
        return rentOrderDetailMapper.selectByExample(example);

    }

    public void deleteById(Integer id) {
        rentOrderDetailMapper.deleteByPrimaryKey(id);
    }

    public Long countMyRead(Integer userId) {
        LitemallRentOrderDetailExample example = new LitemallRentOrderDetailExample();
        example.or().andUserIdEqualTo(userId).andDeletedEqualTo(false);

        return rentOrderDetailMapper.countByExample(example);
    }

    public void deleteByOid(Integer userId, Integer Oid) {
        LitemallRentOrderDetailExample example = new LitemallRentOrderDetailExample();
        example.or().andUserIdEqualTo(userId).andRentOrderIdEqualTo(Oid).andDeletedEqualTo(false);

        rentOrderDetailMapper.deleteByExample(example);
    }

    /**
     * 根据订单id 逻辑删除订单详情表
     *
     * @param Oid
     */
    public void deleteByOid(Integer Oid) {
        LitemallRentOrderDetailExample example = new LitemallRentOrderDetailExample();
        example.or().andRentOrderIdEqualTo(Oid).andDeletedEqualTo(false);

        rentOrderDetailMapper.logicalDeleteByExample(example);
    }

    public LitemallRentOrderDetail queryByUidAndGoodsProductId(Integer userId, Integer goodProductId) {
        LitemallRentOrderDetailExample example = new LitemallRentOrderDetailExample();
        example.or().andUserIdEqualTo(userId).andGoodsProductIdEqualTo(goodProductId).andDeletedEqualTo(false);

        return rentOrderDetailMapper.selectOneByExample(example);
    }
}
